//index.js
//获取应用实例
const app = getApp()
Page({
  data: {
    nullHouse: true, //弹窗默认隐藏
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    register:false,
    userType:0,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindToLogin: function () {
    wx.redirectTo({
      url: '../login/login'
    })
  },
  bindToAgentRegister: function () {
    console.log("欢迎注册")
    app.globalData.userType = 20
    wx.redirectTo({
      url: '../register/register'
    })
  },  
  bindToUserRegister: function () {
    console.log("欢迎注册")
    app.globalData.userType = 10
    wx.redirectTo({
      url: '../register/register'
    })
  },    
  bindToLocal: function () {
    wx.redirectTo({
      url: '../local/local'
    })
  },  
  bindViewTap: function() {
    wx.redirectTo({
      url: '../logs/logs'
    })
  },
  bindRegister:function(){
    console.log("欢迎注册")
  },
  onLoad: function () {

    console.log(app.globalData.splash_current)

    if (app.globalData.splash_url && app.globalData.splash_current == 0){
      console.log("显示Splash")
      console.log(app.globalData.splash_url)
      wx.redirectTo({
        url: '../advs/advs'
      })
    }

    wx.setNavigationBarTitle({ title: '欢迎注册' })

    var that = this;
    var that_app = app;
    var user_openid = wx.getStorageSync('user_openid')

    if (!user_openid) {
      that_app.getPromission()
      wx.login({
        success: function (res) {
          if (res.code) {
            that_app.getUserOpenID(res.code)
            that.setData({
              userInfo: app.globalData.userInfo,
              hasUserInfo: true
            })
            that_app.getUserLoginInfo()
          }
        }
      })
    } else {
      that_app.globalData.userOpenID = user_openid
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    }

    var current_userType = wx.getStorageSync('user_type')
    if (!current_userType) {
      console.log('current_userType undifine')
      // console.log(user_openid)
      that_app.getUserLoginInfo()
    } else {
      if (current_userType == 0 || current_userType == -1) {
        this.setData({
          register: false
        })
      } else {
        console.log('current_userType ' + current_userType)
        this.setData({
          register: true,
          userType: current_userType,
        })
        app.globalData.userType = current_userType
        //根据userType导航到不同的路径
        wx.redirectTo({
          url: '../local/local'
        })
      }
    }
  },
  getUserInfo: function (e) {
    if (app.globalData.userInfo){
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    }
  },
  clickArea: function () {
    var that = this;
    this.setData({
      nullHouse: false, //弹窗显示
    })
    setTimeout(function(){
      that.data.nullHouse = true}
      ,1000)
  },
})
